<?php

namespace app\admin\controller\paotui;

use app\common\controller\Backend;
use think\addons\Service;
use think\Exception;

/**
 * 小程序配置
 *
 * @icon fa fa-circle-o
 */
class Mini extends Backend
{
    
    /**
     * Distanceprice模型对象
     * @var \app\admin\model\paotui\Distanceprice
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        if($this->request->isPost()){
            $params = $this->request->post("row/a");
            if($params){
                foreach ($params as $k=>$v){
                    $configModel = new \app\admin\model\paotui\Config();
                    $configModel->where(['name'=>$k])->update(['value'=>$v]);
                }
            }
            $this->success("请求成功");
        }
        $configModel = new \app\admin\model\paotui\Config();
        $paotuiConfig = $configModel->where(['group'=>['in',['mini','base']]])->column('name,value');
        $this->assign("paotuiConfig",$paotuiConfig);

        return $this->fetch();
    }

    public function pay()
    {
        $info = get_addon_info('epay');
        if(empty($info) || $info['state'] == 0){
            $this->error('请到后台插件管理安装《微信支付宝整合》插件');
        }

        $name = "epay";
        $info = get_addon_info($name);
        $config = get_addon_fullconfig($name);
        if (!$info) {
            $this->error(__('Addon not exists'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a", [], 'trim');

            if ($params) {
                foreach ($config as $k => &$v) {
                    if (isset($params[$v['name']])) {
                        if ($v['type'] == 'array') {
                            $params[$v['name']] = is_array($params[$v['name']]) ? $params[$v['name']] : (array)json_decode($params[$v['name']], true);
                            $value =  $params[$v['name']];
                        } else {
                            $value = is_array($params[$v['name']]) ? implode(',', $params[$v['name']]) : $params[$v['name']];
                        }
                        $v['value'] = $value;
                    }
                }

                try {
                    $addon = get_addon_instance($name);
                    //插件自定义配置实现逻辑
                    if (method_exists($addon, 'config')) {
                        $addon->config($name, $config);
                    } else {
                        //更新配置文件
                        set_addon_fullconfig($name, $config);
                        Service::refresh();
                    }
                } catch (Exception $e) {
                    $this->error(__($e->getMessage()));
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
		// p($config);die;

        $this->view->assign("addon", ['info' => $info, 'config' => $config]);

        return $this->fetch();
    }
}
